<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/25
 * Time: 13:53
 */

namespace App\Process;


use App\Utility\Curl;
use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\EasySwoole\Logger;
use EasySwoole\RedisPool\Redis;
use App\Spider\RedisLogic;
use Swoole\Coroutine;

class GameMatchs extends AbstractProcess
{
    protected function run($arg)
    {
        RedisLogic::clearConsumeMap(); //清除消费队列map
        RedisLogic::clearProduceMap(); //清除生产队列map
        RedisLogic::clearConsumeList();//清除消费队列
        RedisLogic::clearProduceList();//清除生产队列
        RedisLogic::addProduce('https://v0.1zplay.com/api/finish_schedules?category=dota2');
        // TODO: Implement run() method.
        $maxDepth = $arg['maxDepth'];
        $produceListKey = 'easyswooleProduceList';
        $maxCoroutineNum = $arg['maxCoroutineNum'] ?? 3; //最大携程数量

        $urlMap = [
            'finish_dota2' => 'https://v0.1zplay.com/api/finish_schedules?category=dota2',
            'live_dota2'   => 'https://v0.1zplay.com/api/live_schedules?category=dota2',
            'finish_csgo' => 'https://v0.1zplay.com/api/finish_schedules?category=csgo',
            'live_csgo'   => 'https://v0.1zplay.com/api/live_schedules?category=csgo',
            'finish_lol'   => 'https://v0.1zplay.com/api/finish_schedules?category=lol',
            'live_lol'     => 'https://v0.1zplay.com/api/live_schedules?category=lol'
        ];

        for ( $i = 0;$i<$maxCoroutineNum;$i++ ){
            go(function ()use($produceListKey, $maxDepth,$urlMap){
                Redis::invoker('redis', function (\Swoole\Coroutine\Redis $redis) use ($produceListKey,$maxDepth,$urlMap) {
                    while (1) {
                        $data = $redis->lpop($produceListKey);
                        if( empty($data) ){
                            Logger::getInstance()->console('暂无生产队列');
                            Coroutine::sleep(5);
                            continue;
                        }
                        $response = Curl::request('GET',$data['url']);//正在进行lol
                        $http_code = $response->getCurlInfo()['http_code'];
                        if( $http_code == 200 ){
                            $lives = $response->__toString();
                            $lives = json_decode($lives,true);
                            if( !empty($lives) ){
                                RedisLogic::addConsume(['url'=>$data['url'],'data'=>$lives,'type'=>1]);
                                Logger::getInstance()->console("{$data['url']} 已完成");
                            }else{
                                Logger::getInstance()->console("{$data['url']} 暂无比赛");
                            }
                            //GameMatchEvent::produce(['url'=>$data['url'],'data'=>$lives,'type'=>$type]);
                        }else{
                            Logger::getInstance()->console("生产发生错误：{$data['url']}  code:" . $http_code, Logger::LOG_LEVEL_WARNING);
                            if (isset($data['errorNum'])) {
                                $data['errorNum'] += 1;
                            } else {
                                $data['errorNum'] = 1;
                            }
                            if( $data['errorNum'] <= 5 ){
                                RedisLogic::addProduceMap($data['url']);
                                RedisLogic::addProduce($data['url']);
                            }
                        }

                        if( $data['url'] == $urlMap['finish_dota2'] ){
                            RedisLogic::addProduce($urlMap['live_dota2']);
                        }elseif( $data['url'] == $urlMap['live_dota2'] ){
                            RedisLogic::addProduce($urlMap['finish_csgo']);
                        }elseif ($data['url'] == $urlMap['finish_csgo']){
                            RedisLogic::addProduce($urlMap['live_csgo']);
                        }elseif ($data['url'] == $urlMap['live_csgo']){
                            RedisLogic::addProduce($urlMap['finish_lol']);
                        }elseif ($data['url'] == $urlMap['finish_lol']){
                            RedisLogic::addProduce($urlMap['live_lol']);
                        }else{
                            RedisLogic::clearConsumeMap(); //清除消费队列map
                            RedisLogic::clearProduceMap(); //清除生产队列map
                            RedisLogic::addProduce($urlMap['finish_dota2']);
                        }

                        Coroutine::sleep(1);
                    }
                });

            });
        }
    }
}